﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using TAMS;

namespace TAMS.Admin
{
    public partial class Admin_Course : System.Web.UI.MasterPage
    {
        MySqlConnection conn;
        MySqlCommand sqlCommand;
        MySqlDataReader readVals;
        string strProvider = Resource1.databaseConnection;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpCookie tmp = new HttpCookie("LastVisit");
            tmp.Expires = DateTime.Now.AddDays(-1);
            Response.Cookies.Add(tmp);
            Response.Redirect("../login.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("Admin_CourseAdd.aspx");
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            GridView gw = (GridView)(this.ContentPlaceHolder2.FindControl("GridView1"));
            string key = null;

            try
            {
                key = gw.SelectedValue.ToString();
            }
            catch (System.Exception ex)
            {
            	
            }

            if (key != null)
            {
                // Connect to the Database
                conn = new MySqlConnection(strProvider);
                conn.Open();
                string query = "SELECT C.cname FROM Course C WHERE c.cid='" + key+"'";
                sqlCommand = new MySqlCommand(query, conn);
                readVals = sqlCommand.ExecuteReader();
                readVals.Read();
                string name = readVals.GetString("cname");
                Response.Redirect("Admin_CourseModify.aspx" + "?id=" + key +"&name="+name );
                readVals.Close();
            }
            else
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert(\"Select a course !\");</SCRIPT>");
            
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            GridView gw = (GridView)(this.ContentPlaceHolder2.FindControl("GridView1"));
            string key = null;


            try
            {
                key = gw.SelectedValue.ToString();
            }
            catch (System.Exception ex)
            {

            }

            if (key != null)
            {
                // Connect to the Database
                conn = new MySqlConnection(strProvider);
                conn.Open();

                string deletStaff = "DELETE" +
                                    " FROM Course" +
                                    " WHERE cid='"+key+"'";

                sqlCommand = new MySqlCommand(deletStaff, conn);
                readVals = sqlCommand.ExecuteReader();
                Response.Redirect("Admin_Course.aspx");
            }
            else
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert(\"Select a course !\");</SCRIPT>");

        }
    
    }
}